package com.huazi.www.user.cost.servlet;

import com.alibaba.fastjson.JSON;
import com.huazi.www.user.cost.entity.UserEntity;
import com.huazi.www.user.cost.service.UserService;
import com.huazi.www.user.cost.service.impl.UserServiceImpl;
import com.huazi.www.user.cost.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 缴费servlet
 */
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * 接收.jsp界面的请求进行缴费处理
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码
        req.setCharacterEncoding("UTF-8");

        String currPageNo = req.getParameter("currPageNo");

        if (currPageNo == null) {
            currPageNo = "1";
        }
        UserService service = new UserServiceImpl();

        String paymentInsert = req.getParameter("paymentInsert");

        String paymentId = req.getParameter("paymentId");
        if (paymentId == null) {
            paymentId = "1";
        }
        //如果确认缴费则进入判断
        if (paymentInsert != null) {
            Integer paymentId_int = Integer.parseInt(paymentId);
            double money = service.UserMoneySelect_bool(paymentId_int);
            //判断 余额是否大于ct价格
            if (money >= 300) {
                List<UserEntity> list = service.userPayment_bool(paymentId_int);//根据用户id获取我的用户的信息

                UserEntity entity = new UserEntity();//user实体类

                entity.setUserId(list.get(0).getUserId());//给实体类赋值

                entity.setType(list.get(0).getType());//给实体类赋值

                boolean fale = service.userPaymentInsert_bool(paymentId_int, entity);//插入

                boolean faleUpdate = service.userStateUpdate_bool(paymentId_int);//修改

                //异步返回
                PrintWriter writer = resp.getWriter();
                String data = JSON.toJSONString(fale);
                writer.print(data);
                writer.flush();
                writer.close();

            } else {

                PrintWriter writer = resp.getWriter();
                String data = JSON.toJSONString("false");
                writer.print(data);
                writer.flush();
                writer.close();
            }
        } else {
            String name = req.getParameter("name");
            if (name == null) {
                name = "";
            }
            int count = service.UserStateSelect_int();
            Integer currPageNo_int = Integer.parseInt(currPageNo);
            Page<UserEntity> page = service.UsetStateFengYeSelect(name, currPageNo_int);
            req.setAttribute("stateCount", count);
            req.setAttribute("name", name);
            req.setAttribute("userPage", page);
            req.getRequestDispatcher("/Userr/UserPaymentSelect.jsp").forward(req, resp);
        }
    }
}
